package com.linlang.grasharepro.activity.fragment;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.activity.ImageChoose;
import com.linlang.grasharepro.adapter.MyAdapter;
import com.linlang.grasharepro.dao.ExperienceDB;
import com.linlang.grasharepro.dao.ShareCoentenDB;
import com.linlang.grasharepro.model.Experience;
import com.linlang.grasharepro.model.ShareCoenten;
import com.linlang.grasharepro.utils.FileUtiles;
import com.linlang.grasharepro.utils.validation.FireEye;
import com.linlang.grasharepro.utils.validation.FireEyeEnv;
import com.linlang.grasharepro.utils.validation.Form;
import com.linlang.grasharepro.utils.validation.StaticPattern;
import com.linlang.grasharepro.utils.validation.TextViewLoader;
import com.linlang.grasharepro.utils.validation.ValuePattern;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by lixin on 2016-8-6.
 */
public class ReleasePage extends Fragment {

    private View view;
    private LinearLayout gone1, gone2;
    private TextView re_yes, re_no;
    private EditText re_content, re_title;
    private EditText re_year, re_company, re_where, re_workcontent, re_money, re_exrper, re_difftent, tv_contacts, tv_contactWay;
    private ImageView release_upimage, release_image1, release_image2, release_image3, release_image4, release_image5, release_image6, release_image7, release_image8, release_image9;
    private ExperienceDB experienceDB;
    private ShareCoentenDB shareCoentenDB;
    private final int PHOTO_WITH_DATA = 1;
    private FireEye fireEye;//校验类
    private List<String> imgUrls;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.releasepage, container, false);
        initView();
        return view;
    }

    private void initView() {

        View formView = view.findViewById(R.id.form);
        Form form = new Form(formView);

        fireEye = new FireEye(getContext());
        fireEye.add(form.byId(R.id.re_year), StaticPattern.Required);
        fireEye.add(form.byId(R.id.re_company), StaticPattern.Required);
        fireEye.add(form.byId(R.id.re_where), StaticPattern.Required);
        fireEye.add(form.byId(R.id.re_workcontent), StaticPattern.Required);
        fireEye.add(form.byId(R.id.re_money), StaticPattern.Required);
        fireEye.add(form.byId(R.id.re_exrper), StaticPattern.Required);
        fireEye.add(form.byId(R.id.re_difftent), StaticPattern.Required);

        FireEyeEnv.setDebug(true);
        FireEyeEnv.setVerbose(true);

        gone1 = (LinearLayout) view.findViewById(R.id.re_gone1);
        gone2 = (LinearLayout) view.findViewById(R.id.re_gone2);

        RadioGroup yesOrNo = (RadioGroup) view.findViewById(R.id.radioGroupbiye);
//        re_yes = (TextView) view.findViewById(R.id.re_yes);
//        re_no = (TextView) view.findViewById(R.id.re_no);
//        re_yes.setOnClickListener(new MyOnClickListener(0));
//        re_no.setOnClickListener(new MyOnClickListener(3));

        yesOrNo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //获取变更后的选中项的ID
                int radioButtonId = group.getCheckedRadioButtonId();
                //根据ID获取RadioButton的实例
                RadioButton rb = (RadioButton) getActivity().findViewById(radioButtonId);
                //更新文本内容，以符合选中项
                if (rb.getText().toString().equals("是")) {
                    gone1.setVisibility(View.VISIBLE);
                    gone2.setVisibility(View.VISIBLE);
                }else{
                    gone1.setVisibility(View.GONE);
                    gone2.setVisibility(View.GONE);
                }

            }
        });

        //发布的内容和标题
        re_title = (EditText) view.findViewById(R.id.re_title);
        re_content = (EditText) view.findViewById(R.id.re_content);

        //经历名片内容
        re_year = (EditText) view.findViewById(R.id.re_year);
        re_company = (EditText) view.findViewById(R.id.re_company);
        re_where = (EditText) view.findViewById(R.id.re_where);
        re_workcontent = (EditText) view.findViewById(R.id.re_workcontent);
        re_money = (EditText) view.findViewById(R.id.re_money);
        re_exrper = (EditText) view.findViewById(R.id.re_exrper);
        re_difftent = (EditText) view.findViewById(R.id.re_difftent);
        tv_contacts = (EditText) view.findViewById(R.id.re_contant_pe);
        tv_contactWay = (EditText) view.findViewById(R.id.re_contant_way);

        release_upimage = (ImageView) view.findViewById(R.id.release_upimage);
        release_image1 = (ImageView) view.findViewById(R.id.release_image1);
        release_image2 = (ImageView) view.findViewById(R.id.release_image2);
        release_image3 = (ImageView) view.findViewById(R.id.release_image3);
        release_image4 = (ImageView) view.findViewById(R.id.release_image4);
        release_image5 = (ImageView) view.findViewById(R.id.release_image5);
        release_image6 = (ImageView) view.findViewById(R.id.release_image6);
        release_image7 = (ImageView) view.findViewById(R.id.release_image7);
        release_image8 = (ImageView) view.findViewById(R.id.release_image8);
        release_image9 = (ImageView) view.findViewById(R.id.release_image9);

        TextView release = (TextView) view.findViewById(R.id.tv_release);
        release.setOnClickListener(new MyOnClickListener(1));
        release_upimage.setOnClickListener(new MyOnClickListener(2));
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            switch (index) {
                case 0:
                    gone1.setVisibility(View.VISIBLE);
                    gone2.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    fireEye.dump();
                    if (!fireEye.test(true).passed) {
                        return;
                    }
                    Experience experience = new Experience();
                    experience.setCompany(re_company.getText().toString());
                    experience.setGraduationTime(re_year.getText().toString());
                    experience.setContent(re_workcontent.getText().toString());
                    experience.setExperince(re_exrper.getText().toString());
                    experience.setPosition(re_where.getText().toString());
                    experience.setTreatment(re_money.getText().toString());
                    experience.setDifferent(re_difftent.getText().toString());
                    experience.setRelation(tv_contacts.getText().toString());
                    experience.setRelationWay(tv_contactWay.getText().toString());

                    //新增经历名片
                    experienceDB = new ExperienceDB(getContext());
                    experienceDB.open();
                    int experienceId = experienceDB.insertExperience(experience);

                    ShareCoenten shareCoenten = creatShareContent(experienceId + "");

                    //新增分享内容
                    shareCoentenDB = new ShareCoentenDB(getContext());
                    shareCoentenDB.open();
                    shareCoentenDB.createShareCoenten(shareCoenten);
                    Toast.makeText(getContext(), "发布成功", Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Intent in = new Intent(getActivity(), ImageChoose.class);
                    startActivityForResult(in, PHOTO_WITH_DATA);
                    break;
                case 3:
                    gone1.setVisibility(View.GONE);
                    gone2.setVisibility(View.GONE);
                    break;
            }

        }
    }

    public ShareCoenten creatShareContent(String experienceId) {
        //获取用户登陆信息
        SharedPreferences preferences = getActivity().getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");
        String type = preferences.getString("type", "");
        String major = preferences.getString("major", "");
        String headUrl = preferences.getString("headUrl", "");

        ShareCoenten shareCoenten = new ShareCoenten();
        shareCoenten.setUserName(account);
        shareCoenten.setMajor(major);
        shareCoenten.setType(type);
        shareCoenten.setHeadUrl(headUrl);
        shareCoenten.setContent(re_content.getText().toString());
        shareCoenten.setTitle(re_title.getText().toString());
        shareCoenten.setExperienceId(experienceId);

        try {
            Date now = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            String date = format.format(now);
            shareCoenten.setTime(date);
        } catch (Exception e) {
        }

        if (imgUrls != null) {
            for (int i = 0; i < imgUrls.size(); i++) {
                switch (i) {
                    case 0:
                        shareCoenten.setImgUrl(imgUrls.get(i));
                        continue;
                    case 1:
                        shareCoenten.setImgUrl1(imgUrls.get(i));
                        continue;
                    case 2:
                        shareCoenten.setImgUrl2(imgUrls.get(i));
                        continue;
                    case 3:
                        shareCoenten.setImgUrl3(imgUrls.get(i));
                        continue;
                    case 4:
                        shareCoenten.setImgUrl4(imgUrls.get(i));
                        continue;
                    case 5:
                        shareCoenten.setImgUrl5(imgUrls.get(i));
                        continue;
                    case 6:
                        shareCoenten.setImgUrl6(imgUrls.get(i));
                        continue;
                    case 7:
                        shareCoenten.setImgUrl7(imgUrls.get(i));
                        continue;
                    case 8:
                        shareCoenten.setImgUrl8(imgUrls.get(i));
                        continue;
                }
            }
        }
        return shareCoenten;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("size:::::::::::::::::", MyAdapter.mSelectedImage.size() + "");
        if (resultCode == getActivity().RESULT_OK) {
            imgUrls = new ArrayList<String>();
            for (int i = 0; i < MyAdapter.mSelectedImage.size(); i++) {
                imgUrls.add(MyAdapter.mSelectedImage.get(i));
                switch (i) {
                    case 0:
                        release_image1.setVisibility(View.VISIBLE);
                        release_image1.setImageBitmap(FileUtiles.getSmallBitmap(MyAdapter.mSelectedImage.get(i)));
                        continue;
                    case 1:
                        release_image2.setVisibility(View.VISIBLE);
                        release_image2.setImageBitmap(FileUtiles.getSmallBitmap(MyAdapter.mSelectedImage.get(i)));
                        continue;
                    case 2:
                        release_image3.setVisibility(View.VISIBLE);
                        release_image3.setImageBitmap(FileUtiles.getSmallBitmap(MyAdapter.mSelectedImage.get(i)));
                        continue;
                    case 3:
                        release_image4.setVisibility(View.VISIBLE);
                        release_image4.setImageBitmap(FileUtiles.getSmallBitmap(MyAdapter.mSelectedImage.get(i)));
                        continue;
                    case 4:
                        release_image5.setVisibility(View.VISIBLE);
                        release_image5.setImageBitmap(FileUtiles.getSmallBitmap(MyAdapter.mSelectedImage.get(i)));
                        continue;
                    case 5:
                        release_image6.setVisibility(View.VISIBLE);
                        release_image6.setImageBitmap(FileUtiles.getSmallBitmap(MyAdapter.mSelectedImage.get(i)));
                        continue;
                    case 6:
                        release_image7.setVisibility(View.VISIBLE);
                        release_image7.setImageBitmap(FileUtiles.getSmallBitmap(MyAdapter.mSelectedImage.get(i)));
                        continue;
                    case 7:
                        release_image8.setVisibility(View.VISIBLE);
                        release_image8.setImageBitmap(FileUtiles.getSmallBitmap(MyAdapter.mSelectedImage.get(i)));
                        continue;
                    case 8:
                        release_image9.setVisibility(View.VISIBLE);
                        release_image9.setImageBitmap(FileUtiles.getSmallBitmap(MyAdapter.mSelectedImage.get(i)));
                        continue;
                }
            }
            MyAdapter.mSelectedImage.clear();
        }
    }

}
